package com.cloud.lawcase.api.provider;

import com.cloud.lawcase.api.bean.LawItemSearchCriteria;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.util.StringUtils;

import com.cloud.lawcase.api.bean.LawCauseSearchCriteria;

public class TcDicLawItemPageProvider {
	private static final String LIST_PAGE_SELECT = "t1.law_id, t1.law_title, t2.item_id, t2.item_code, t2.item_name, t2.item_content",
			LIST_PAGE_FOMR = "tc_dic_law t1, tc_dic_law_item t2";

	public String listPage(LawCauseSearchCriteria bean) {
		SQL sql = new SQL().SELECT(LIST_PAGE_SELECT).FROM(LIST_PAGE_FOMR);
		sql.WHERE("t1.law_id = t2.law_id and t1.sx_id = 1");
		if (!StringUtils.isEmpty(bean.getLawTitle())) {
			bean.setLawTitle("%" + bean.getLawTitle() + "%");
			sql.WHERE("t1.law_title like #{lawTitle}");
		}
		if (!StringUtils.isEmpty(bean.getItemContent())) {
			bean.setItemContent("%" + bean.getItemContent() + "%");
			sql.WHERE("t2.item_content like #{itemContent}");
		}
		return sql.toString();
	}

	public String list(LawItemSearchCriteria bean) {
		SQL sql = new SQL().SELECT(LIST_PAGE_SELECT).FROM(LIST_PAGE_FOMR);
		sql.WHERE("t1.law_id = t2.law_id and t1.sx_id = 1 and t2.item_name is not null");

		if (!StringUtils.isEmpty(bean.getLawTitle())) {
			bean.setLawTitle("%" + bean.getLawTitle() + "%");
			sql.WHERE("t1.law_title like #{lawTitle}");
		}
		if (bean.getLawID()!=null) {
			sql.WHERE("t1.law_id = #{lawID}");
		}
		if (bean.getArticleID()!=null) {
			sql.WHERE("t2.article_id = #{articleID}");
		}
		if (bean.getParagraphID()!=null) {
			sql.WHERE("t2.paragraph_id = #{paragraphID}");
		}
		if (bean.getSubParagraphID()!=null) {
			sql.WHERE("t2.sub_paragraph_id = #{subParagraphID}");
		}
		if (bean.getItemID()!=null) {
			sql.WHERE("t2.entry_id = #{itemID}");
		}
		return sql.toString();
	}
}
